//
//  CPPopupController.swift
//  CJPackDemo
//
//  Created by 陈警卫 on 2023-12-07.
//

import UIKit
import CJPack
import SnapKit

class CPPopDetailView: UIView {
    
    var cancelB: UIButton!
    var sureB: UIButton!
    
    var didSureAction: (() -> ())?
    var didCancelAction: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .white
        self.cj_corner(conrners: [.topLeft, .topRight], radius: 15)
        setupView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        kCJLog("\(self)--deinit")
    }
    
    func setupView() {
        
        let bw = (cj_width - 25 * 2 - 20) * 0.5
        
        cancelB = UIButton(type: .custom)
        cancelB.backgroundColor = .cj_color("E5E5E5")
        cancelB.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        cancelB.setTitle("Cancel", for: .normal)
        cancelB.setTitleColor(.black, for: .normal)
        addSubview(cancelB)
        cancelB.snp.makeConstraints { make in
            make.left.equalTo(25)
            make.width.equalTo(bw)
            make.bottom.equalTo(-CJScreen.tabBottomHeight - 20)
            make.height.equalTo(45)
        }
        
        sureB = UIButton(type: .custom)
        sureB.backgroundColor = .cj_color("DD2526")
        sureB.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        sureB.setTitle("Sure", for: .normal)
        sureB.setTitleColor(.black, for: .normal)
        addSubview(sureB)
        sureB.snp.makeConstraints { make in
            make.right.equalTo(-25)
            make.top.height.width.equalTo(cancelB)
        }
        
        cancelB.layer.cornerRadius = 45 * 0.5
        sureB.layer.cornerRadius = 45 * 0.5
        
        cancelB.addTarget(self, action: #selector(cancelBAction), for: .touchUpInside)
        sureB.addTarget(self, action: #selector(sureBAction), for: .touchUpInside)
        
    }
    
    @objc func cancelBAction() {
        if let didCancelAction = didCancelAction {
            didCancelAction()
        }
    }
    
    @objc func sureBAction() {
        if let didSureAction = didSureAction {
            didSureAction()
        }
    }
    
}

class CPPopupController: CJBaseBackController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let popuoB1 = UIButton(type: .custom)
        popuoB1.frame = CGRect(x: 20, y: CJScreen.navBatBarHeight + 10, width: 200, height: 30)
        popuoB1.backgroundColor = CPColor.vistaModel.color
        popuoB1.setTitle("pop弹出 .popup", for: .normal)
        popuoB1.setTitleColor(CPColor.foreModel.color, for: .normal)
        popuoB1.addTarget(self, action: #selector(popuoB1Action(btn:)), for: .touchUpInside)
        view.addSubview(popuoB1)
    }
    
    @objc func popuoB1Action(btn: UIButton) {
        
        let detailView = CPPopDetailView(frame: CGRect(x: 0, y: 0, width: view.cj_width, height: 400))
        
        let popup = CJPopupView.popup(detailView, mode: .show)
        popup?.isCoverEnable = false

        detailView.didCancelAction = { [weak popup] in
            popup?.hideMainView()
        }
    }

}
